package org.jackysoft.entity;

import java.util.Collection;

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.jackysoft.annotations.Id;
import org.jackysoft.annotations.Transient;
import org.jackysoft.bean.define.EntitySchemas;
import org.jackysoft.entity.base.AbstractRootEntity;
import org.jackysoft.util.SecurityGrade;

import com.google.common.collect.Lists;
import com.google.gson.annotations.Expose;



public class User extends AbstractRootEntity implements Comparable<User> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6758732902889482666L;

	private String password = "12345678";

	private String repassword;
	@Expose
	private String localName;

	private boolean enabled = true;

	private Department department;

	private Role role;

	private Collection<SecurityRole> authoritys = Lists.newArrayList();
	private SecurityRole primarySecurole;
	@Expose
	private String username;
	private SecurityGrade securityGrade = SecurityGrade.非密;
		
	public User() {
		
	}
	

	// for add new user
	public User(String username, String password, Department dept, Role role,
			SecurityRole securole) {
		this.username = username;
		this.password = password;
		this.department = dept;
		this.role = role;
		this.primarySecurole = securole;
		this.getAuthoritys().add(securole);
	}

	public User(String username) {
		this.username = username;
	}

	public User(String username, String localName, boolean enabled) {
		this.username = username;
		this.localName = localName;
		this.setEnabled(enabled);
	}
	
	public User(String username, String localName, int sort, boolean enabled) {
		this.username = username;
		this.localName = localName;
		this.setEnabled(enabled);
		this.sort = sort;
	}

	public User(String username, String local, Department dept, Role role,
			boolean enabled) {
		this.username = username;
		this.localName = local;
		this.department = dept;
		this.role = role;
		this.setEnabled(enabled);
	}

	
	public Role getRole() {
		return role;
	}

	/**
	 * @param role
	 *            要设置的 role
	 */
	public void setRole(Role role) {
		this.role = role;
	}

	
	public Department getDepartment() {
		return department;
	}

	/**
	 * @param department
	 *            要设置的 department
	 */
	public void setDepartment(Department department) {
		this.department = department;
	}

	/**
	 * @return localName
	 */
	@NotNull(message = "中文名必须提供")
	public String getLocalName() {
		return localName;
	}

	/**
	 * @param localName
	 *            要设置的 localName
	 */
	public void setLocalName(String localName) {
		this.localName = localName;
	}

	/**
	 * @param password
	 *            要设置的 password
	 */
	public void setPassword(String password) {
		this.password = password;
	}


	/**
	 * @param enabled
	 *            要设置的 enabled
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	
	
		
	public Collection<SecurityRole> getAuthoritys() {
		return authoritys;
	}

	public void setAuthoritys(Collection<SecurityRole> authoritys) {
		this.authoritys = authoritys;
	}

	// ~ Methods
	// ========================================================================================================

	@Override
	public boolean equals(Object rhs) {
		if (!(rhs instanceof User) || (rhs == null)) {
			return false;
		}
		User user = (User) rhs;
		return this.getUsername().equalsIgnoreCase(user.getUsername());
	}

	
	@NotNull(message = "密码必须提供")
	@Size(min = 6, max = 20, message = "密码必须在6-20位之间")
	public String getPassword() {
		return password;
	}

	@AssertTrue(message = "两次密码必须相同")
	@Transient
	public boolean isPasswordEquals() {
		if (null == getPassword() || null == getRepassword())
			return false;
		return this.getPassword().equals(getRepassword());
	}

	
	@Id	
	
	@NotNull(message = "登录名必须提供")
	@Size(min = 4, max = 15, message = "登录名必须在4-15位之间")
	@Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "登录名必须是英文半角字符")
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public int hashCode() {
		if (username == null)
			return 0;
		return this.username.hashCode();
	}

	@Override
	public int compareTo(User o) {
		if (o == null)
			return 1;
		return sort - o.sort;
	}

	public void setPrimarySecurole(SecurityRole primarySecurole) {
		this.primarySecurole = primarySecurole;
	}

	
	public SecurityRole getPrimarySecurole() {
		return primarySecurole;
	}

	@Transient
	@NotNull(message = "确认密码必须提供")
	@Size(min = 6, max = 20, message = "确认密码必须在6-20位之间")
	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public SecurityGrade getSecurityGrade() {
		return securityGrade;
	}

	public void setSecurityGrade(SecurityGrade securityGrade) {
		this.securityGrade = securityGrade;
	}


	public boolean isEnabled() {
		return enabled;
	}
	
	
	
}
